package elka.gis.postman.bean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Graf
 * @author Wojtek
 *
 */
public class Graph {
	
	private List<Vertex> vertices;
	private List<Edge> edges;

	public List<Vertex> getVertices() {
		return vertices;
	}

	public void setVertices(List<Vertex> vertices) {
		this.vertices = vertices;
	}
	
	public List<Edge> getEdges() {
		return edges;
	}

	public void setEdges(List<Edge> edges) {
		this.edges = edges;
	}

	/**
	 * Zwraca liste wierzcholkow o nieparzystym stopniu
	 * @return
	 */
	public List<Vertex> getAllVertexOddDegree() {
		List<Vertex> vertexOddDegree = new ArrayList<Vertex>();
		
		for(Vertex vertex : vertices) {
			if(vertex.isOddDegree()) {
				vertexOddDegree.add(vertex);
			}
		}
		
		return vertexOddDegree;
	}
	
	public Graph copy(){
		Graph g = new Graph();
		Map<Integer, Vertex> verticesMap = new HashMap<Integer, Vertex>();
		for(Vertex v : getVertices()) {
			verticesMap.put(v.getId(), v.copyWithoutEdges());
		}
		List<Edge> edges = new ArrayList<Edge>();
		for(Edge edge: getEdges()){
			Edge e = new Edge();
			Vertex startPoint = verticesMap.get(edge.getStartPoint().getId());
			Vertex endPoint = verticesMap.get(edge.getEndPoint().getId());
			e.setLength(edge.getLength());
			
			e.setStartPoint(startPoint);
			e.setEndPoint(endPoint);
			startPoint.addEdge(e);
			endPoint.addEdge(e);
			
			edges.add(e);
		}
		
		g.setEdges(edges);
		g.setVertices(new ArrayList<Vertex>(verticesMap.values()));
		return g;
	}
	
	
	

};
